import Vue from 'vue'
import Vuex from 'vuex'
// 导入本地token持久化的方法
import * as auth from '../utils/auth.js'
// 导入登录接口的请求方法
import { login } from '../api/user'
import chat from './modules/chat'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: auth.getToken() || {}
  },
  mutations: {
    // 存
    setToken (state, token) {
      // 存到内存
      state.user = token
      // 存到硬盘
      auth.setToken(token)
    },
    // 删
    delToken (state) {
      state.user = {}
      auth.delToken()
    }
  },
  actions: {
    // 登录接口请求
    async getToken ({ commit }, form) {
      const { data } = await login(form)
      // console.log(status, data)
      commit('setToken', data)
    }
  },
  modules: {
    chat
  }
})
